package com.woniuxy.project.campus.filter;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

@Component
@Order
@ConfigurationProperties(prefix = "whitelist")
@Data
public class LoginFilter implements GlobalFilter {
    private List<String> whitelist;
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        RequestPath routeInformation = exchange.getRequest().getPath();
        System.out.println(routeInformation);
        //获取请求对象
        System.out.println("***************************************************************");
        ServerHttpRequest request = exchange.getRequest();
        //获取请求路径
        String path = request.getURI().getPath();
        System.out.println(path);
        //判读swagger接口
//        if(path.indexOf("/swagger")!=-1) {
//            return chain.filter(exchange);
//        }
//        如果是白名单中的路径，直接放行
//       if (!whitelist.contains(path)){
//           //获取请求头中的token
//           String token = request.getHeaders().getFirst("token");
//           if (token==null||token.equals("")){
//               //没有token，拦截
//               return null;
//           }
//           return chain.filter(exchange);
//       }
        return chain.filter(exchange);
    }
}
